package com.qm.controller;

import com.qm.service.FileService;
import com.qm.vo.ImageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 @author james
 @create 2021-03-23 15:12
 */
@RestController
public class FileController {

    @Autowired
    private FileService fileService;

    /**
     * 业务说明：实现用户文件上传
     * url地址：http://localhost:8090/file
     * 请求参数：fileImage
     * 返回值：操作成功的字符串
     */
    @RequestMapping("/file")
    public String file(MultipartFile fileImage) throws IOException {
        String dir = "D:/QM-PICTURE/images";
        File dirFile = new File(dir);
        if (!dirFile.exists()) {
            dirFile.mkdirs();   //创建多级目录
        }
        //获取文件名称
        String fileName = fileImage.getOriginalFilename();
        //将文件封装为一个完整的路径
        File imageFile = new File(dir+"/"+fileName);
        //接口中提供一个方法 实现文件上传  outputStream 实现写盘操作
        fileImage.transferTo(imageFile);
        return "上传成功";

    }

    @RequestMapping("/pic/upload")
    public ImageVO upLoadFile(MultipartFile file) {
        return fileService.uploadFile(file);
    }

}
